﻿Ext.define("PSI.Accounting.AccountTitleAdmin.AccountTitleEdit", {

    createUI: function (data) {

        var me = this;
        var Addbasecodewindow = Ext.create("Ext.window.Window", {
            title: data == null ? "新增科目" : "编辑科目",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 400,
            height: 200,
            border: false,
            layout: "fit",
            items: [
                 {
                     id: "editForm",
                     xtype: "form",
                     layout: "form",
                     height: "100%",
                     bodyPadding: 5,
                     defaultType: 'textfield',
                     fieldDefaults: {
                         labelWidth: 70,
                         labelAlign: "right",
                         labelSeparator: "",
                         msgTarget: 'side'
                     },
                     items: [
                           {
                               xtype: "hidden",
                               name: "ATId",
                               value: data == null ?null:data.id
                           },
                           {
                               fieldLabel: "科目码",
                               allowBlank: false,
                               blankText: "没有输入名称",
                               beforeLabelTextTpl: PSI.Const.REQUIRED,
                               name: "ATCode",
                               value: data == null ? null : data.accountTitleCode
                           },
                           {
                               fieldLabel: "科目名称",
                               allowBlank: false,
                               blankText: "没有输入名称",
                               beforeLabelTextTpl: PSI.Const.REQUIRED,
                               name: "ATName",
                               value: data == null ? null : data.accountTitleName
                           },
                           {
                               xtype: 'combobox',
                               fieldLabel: "科目分类",
                               store: Ext.create('Ext.data.Store', {
                                   fields: ['abbr', 'name'],
                                   data : [
                                       {  "name": "资产" },
                                       {  "name": "负债" },
                                       {  "name": "所有者权益" },
                                       { "name": "损益" },
                                       { "name": "成本" }
                                    
                                   ]
                               }),
                               displayField: 'name',
                               valueField: 'name',
                               editable:false,
                               value: data == null ? '资产' : data.accountTitleCategory,
                               queryMode: 'local',
                               name: "ATCategory"
                              // renderTo: Ext.getBody()
                           },
                           {
                               xtype:'checkbox',
                               fieldLabel: "末级科目",
                               name: "IsLeaf",
                               checked: data == null ? false : data.isLeaf=="1"?true:false
                           },
                           {
                               xtype: 'psi_accounting_accounttileadmin_bcmetadatafield',
                               id:'kemu',
                               fieldLabel: "业务分类",
                               allowBlank: false,
                               beforeLabelTextTpl: PSI.Const.REQUIRED,
                               value: data == null ? null : data.text
                           }, {
                               xtype: 'hidden',
                               id: 'bizCategoryId',
                               name: 'bizCategoryId',
                               value: data == null ? null : data.bizCategoryId
                           }
                     ],
                     buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: this.onOK,
                            scope: this
                        },
                        {
                            text: "取消", handler: function () {
                                var me = this;
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.addbasecodewindow.close();
                                });
                            }, scope: this
                        }
                     ]
                 }

            ]

        }).show();
        me.addbasecodewindow = Addbasecodewindow;

    },

    show: function (data) {

        return this.createUI(data);

    },

    onOK: function () {
        var me = this;
        var at = Ext.getCmp("accountTitletree");
        var win = this.addbasecodewindow
        var f = Ext.getCmp("editForm");
        var el = f.getEl();
        el.mask("数据保存中...");
        f.submit({
            url: "/AccountTitleAdmin/AddAccountTitle",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                var cc = action.result.success;
                if (cc) {
                    PSI.MsgBox.showInfo("数据保存成功", function () {
                        win.close();
                        at.getStore().load();
                    });
                }
                else {
                    Ext.Msg.alert('Failure', action.result.msg);
                }
            },
            failure: function (form, action) {
                el.unmask();
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('Failure', 'ajax链接失败!');
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        Ext.Msg.alert('Failure', action.result.msg);
                }
            }
        });

    }


})